package com.ihr360.payroll.service.salarytasklet.step.processor;

import com.ihr360.job.core.annotation.BeforeProcess;
import com.ihr360.job.core.item.ItemProcessor;
import com.ihr360.job.core.repository.JobRepository;
import com.ihr360.job.core.scope.context.SimpleStepContext;
import com.ihr360.sharedservice.model.StatutoryVacation;
import com.ihr360.sharedservice.model.WorkingCalendar;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author davidwei
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class WorkingCalendarProcessor implements ItemProcessor<List<StatutoryVacation>, WorkingCalendar> {
    private SimpleStepContext simpleStepContext;
    @Autowired
    private JobRepository jobRepository;

    @Override
    public WorkingCalendar process(List<StatutoryVacation> item) throws Exception {
        if (item == null) {
            jobRepository.error(simpleStepContext, "[工作日历]", "无法获取工作日历");
            ValidationUtils.reject(null, "无法获取工作日历");
        }
        if (item!=null && item.size() == 0) {
            jobRepository.info(simpleStepContext, "[工作日历]", "当前周期内不存在公休假和补偿假期");
        }
        Map<Date, StatutoryVacation> holidayCalendar = item.stream().collect(Collectors.toMap(s -> s.getVacationDate(), s -> s));
        WorkingCalendar workingCalendar = new WorkingCalendar(holidayCalendar);
        return workingCalendar;
    }

    @BeforeProcess
    public void setSimpleStepContext(List<StatutoryVacation> vacations, SimpleStepContext simpleStepContext) {
        this.simpleStepContext = simpleStepContext;
    }
}